package com.graduate.front.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.graduate.common.entity.Account;
import com.graduate.common.entity.Handle;
import com.graduate.common.entity.House;
import com.graduate.common.entity.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author bwzfy
 * @create 2021/5/15
 **/
@Controller
@RequestMapping("/account")
public class AccountController extends BaseController{


    @ResponseBody
    @GetMapping("/handle/{id}")
    public Result<String> account(@PathVariable("id") String id) {
        // 先从数据库中把办理表的数据查询出来
        Handle handle = handleService.getById(id);
        if (handle.getStatus() == 1) {
            Result<String> result = new Result<String>().failure();
            result.setData("不能重复结账");
            return result;
        }
        UpdateWrapper<Handle> wrapper = new UpdateWrapper<>();
        // 把办理表中的这条数据的状态设置为已导出
        wrapper.set("status", 1);
        wrapper.eq("mid", id);
        boolean update = handleService.update(wrapper);
        if (update) {
            // 创建account对象
            Account account = new Account();
            account.setIdCard(handle.getIdentificationNum());
            account.setRealName(handle.getRealName());
            account.setHid(handle.getHid());
            account.setStartTime(handle.getStartTime());
            account.setEndTime(handle.getEndTime());
            // 计算消费
            House house = houseService.getById(handle.getHid());
            double price = 0;
            if (handle.getMethod() == 0) {
                // 如果是0就是普通订房
                price = house.getHousePrice();
            } else if (handle.getMethod() == 1) {
                // ip订房
                price = house.getVipPrice();
            } else if (handle.getMethod() == 2) {
                // 组队购房
                price = house.getGroupPrice();
            }
            // 计算时长
            long l = handle.getEndTime().getTime() - handle.getStartTime().getTime();
            account.setCost(l / 1000 / 60 / 60 / 24 * price);
            accountService.save(account);
            return new Result<String>().success();
        }else {
            Result<String> result = new Result<String>().failure();
            result.setData("结账失败");
            return result;
        }
    }

}
